﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Sales;
using ESOL.Business.Mst;
using ESOL.Business.Prod;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainProductPlanOrder : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        private ProdPlanOrderEntity mSelectedPlanOrder;

        #endregion

        #region ctors
        public frmMaintainProductPlanOrder()
        {
            InitializeComponent();
        }

        public frmMaintainProductPlanOrder(ProdPlanOrderEntity m)
        {
            InitializeComponent();
            mSelectedPlanOrder = m;
        }

        #endregion

        #region functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PSTATUS", false);

            DataTable dtIsMark = new DataTable();
            dtIsMark.Columns.Add("name");
            dtIsMark.Columns.Add("value");
            dtIsMark.Rows.Add("是", 1);
            dtIsMark.Rows.Add("否", 0);
            cmbIsMark.DataSource = dtIsMark;
            cmbIsMark.DisplayMember = "name";
            cmbIsMark.ValueMember = "value";
        }

        private void SetField()
        {
            if (mSelectedPlanOrder != null)
            {
                txtProductPlanOrder.Text = mSelectedPlanOrder.PlanOrderCode;
                cmbStatus.SetSelectedValue(mSelectedPlanOrder.Status);
                dtpPlanDelivery.EditValue = mSelectedPlanOrder.DeliveryDate;
                txtPlanQty.Text = mSelectedPlanOrder.Qty.ToString();
                SalesProdPlanApplicationItemEntity mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(mSelectedPlanOrder.ApplicationItemId);
                if (mApplicationItem == null)
                    return;
                MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mApplicationItem.ProductId);
                if (mProduct == null)
                    return;
                txtProductName.Text = mProduct.ProductName;
                txtContractModel.Text = mApplicationItem.ProductSpecOut;
                txtDescription.Text = mProduct.Description;
                txtRemark.Text = mSelectedPlanOrder.Remark;
                cmbIsMark.SetSelectedValue(mSelectedPlanOrder.IsMark);
            }
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(dtpPlanDelivery.EditValue.ToString()) == true)
            {
                MessageBox.Show("计划交期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dtpPlanDelivery.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtPlanQty.Text) == true)
            {
                MessageBox.Show("计划数量不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPlanQty.Focus();
                return false;
            }
            return true;
        }

        private void SetEntity()
        {
            if (mSelectedPlanOrder != null)
            {
                mSelectedPlanOrder.DeliveryDate = (DateTime)dtpPlanDelivery.EditValue;
                mSelectedPlanOrder.Qty = Convert.ToDecimal(txtPlanQty.Text);
                mSelectedPlanOrder.IsMark = Convert.ToInt32(cmbIsMark.SelectedValue.ToString());
                mSelectedPlanOrder.Status = cmbStatus.SelectedValue.ToString();
                mSelectedPlanOrder.Remark = txtRemark.Text;
            }
        }

        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            SetField();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckData() == false)
                return;
            SetEntity();
            if (ProdPlanOrderBs.UpdateProdPlanOrder(mSelectedPlanOrder))
            {
                MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                return;
            }
            else
            {
                MessageBox.Show("保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        #endregion


        private void txtProductPlanOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanOrderQuery frm = new frmMaintainProductPlanOrderQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanOrder != null)
            {
                txtProductPlanOrder.Text = frm.SelectedProdPlanOrder.PlanOrderCode;
                txtProductPlanOrder.Tag = frm.SelectedProdPlanOrder;
            }
        }
    }
}
